package eerbaar.v2.menu.graphics;

import eerbaar.v2.graphics.rect.RectAlign;
import eerbaar.v2.graphics.rect.RectAlignOpposite;
import eerbaar.v2.graphics.rect.RectMove;
import eerbaar.v2.graphics.rect.RectRelation;
import eerbaar.v2.graphics.rect.Rectangle2;

public class PositionFinder {

    private final ViewPort viewPort;
    private final Rectangle2 ancor;

    public PositionFinder(ViewPort viewPort, Rectangle2 ancor) {
        this.viewPort = viewPort;
        this.ancor = ancor;
    }

    public Rectangle2 position(Rectangle2 cursor) {

        // move the cursor outside the anchor
        cursor = RectAlignOpposite.moveToBottomSide(this.ancor, cursor);
        cursor = RectMove.moveDown(cursor, 25);

        // if the cursor is off the screen, move it to the left side.
        if (RectRelation.isOutside(this.viewPort, cursor)) {

            cursor = RectAlignOpposite.moveToRightSide(this.ancor, cursor);
            cursor = RectAlign.alignBottom(this.ancor, cursor);
            cursor = RectMove.moveUp(cursor, 1);
        }

        // if the cursor is off the screen, move it to the left side.
        if (RectRelation.isOutside(this.viewPort, cursor)) {

            cursor = RectAlignOpposite.moveToLeftSide(this.ancor, cursor);
            cursor = RectAlign.alignBottom(this.ancor, cursor);
        }

        return cursor;
    }

}
